// src/utils/formatters.js
export const formatCopywriting = (content, platform) => {
  if (!content) return ''

  let formattedContent = content
    .replace(/\n\n+/g, '</p><p>')
    .replace(/\n/g, '<br>')

  switch (platform) {
    case 'wechat':
      // 微信公众号格式：标题、段落、强调、排版优化
      formattedContent = formattedContent
        // 处理一级标题
        .replace(/^#\s+(.*?)$/gm, '<h1 class="wechat-main-title">$1</h1>')
        // 处理二级标题
        .replace(/^##\s+(.*?)$/gm, '<h2 class="wechat-subtitle">$1</h2>')
        // 处理【标题】格式
        .replace(/【(.*?)】/g, '<strong class="wechat-title">$1</strong>')
        // 处理内容分段
        .replace(/^(标题：|导语：|正文：|结尾：)/gm, '<div class="wechat-section">$1</div>')
        // 处理列表
        .replace(/^(\d+)\.\s+(.*?)$/gm, '<div class="wechat-list-item"><span class="wechat-list-number">$1.</span> $2</div>')
        // 中英文之间添加空格
        .replace(/([\u4e00-\u9fa5])([a-zA-Z])/g, '$1 $2')
        .replace(/([a-zA-Z])([\u4e00-\u9fa5])/g, '$1 $2')
      break

    case 'weibo':
      // 微博格式：话题标签高亮、排版优化
      formattedContent = formattedContent
        // 话题标签高亮
        .replace(/#([^#]+)#/g, '<span class="weibo-hashtag">#$1#</span>')
        // 强调内容
        .replace(/【(.*?)】/g, '<strong class="weibo-highlight">$1</strong>')
        // 中英文之间添加空格
        .replace(/([\u4e00-\u9fa5])([a-zA-Z])/g, '$1 $2')
        .replace(/([a-zA-Z])([\u4e00-\u9fa5])/g, '$1 $2')
      break
    case 'douyin':
      formattedContent = formattedContent
        .replace(/^(视频脚本|背景音乐|画面描述|旁白|字幕)：/gm, '<div class="douyin-section">$1：</div>')
        .replace(/【(.*?)】/g, '<strong class="douyin-highlight">$1</strong>')
        .replace(/#挑战([^#]+)#/g, '<span class="douyin-challenge">#挑战$1#</span>')
        .replace(/([\u4e00-\u9fa5])([a-zA-Z])/g, '$1 $2')
        .replace(/([a-zA-Z])([\u4e00-\u9fa5])/g, '$1 $2')
      break
  }

  if (!formattedContent.startsWith('<p>') && !formattedContent.startsWith('<div') && !formattedContent.startsWith('<h1') && !formattedContent.startsWith('<h2')) {
    formattedContent = `<p>${formattedContent}</p>`
  }

  return formattedContent
}

export const formatHashtags = (hashtags) => {
  if (!hashtags || !Array.isArray(hashtags)) return []
  return hashtags.map(tag => 
    typeof tag === 'string' ? (tag.startsWith('#') ? tag : `#${tag}`) : tag
  )
}